package com.run.cat.api.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.run.cat.common.base.BaseResult;
import com.run.cat.common.constants.RedisConstants;
import com.run.cat.common.redis.RedisUtil;
import com.run.cat.common.security.JWTToken;
import com.run.cat.common.util.EmptyUtil;
import com.run.cat.common.util.JWTUtil;
import com.run.cat.dao.dto.RegisterDto;
import com.run.cat.dao.dto.SmsDto;
import com.run.cat.dao.entity.Register;
import com.run.cat.service.IRegisterService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author CG
 * @since 2020-08-19
 */
@Controller
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IRegisterService registerService;

    private String jwtTime= "86400000";

    public static final  long TOKEN_LOSE=1000*60*60*24;//redis token有效期

    @RequestMapping("/login")
    @ResponseBody
    public BaseResult login(@Valid @RequestBody RegisterDto dto, BindingResult result) throws UnsupportedEncodingException {
        if (result.hasErrors())
            return BaseResult.failMsg(result.getFieldError().getDefaultMessage());
        String encryption_type = "MD5";
        int hashIterations = 3;
        String token = "";
        EntityWrapper wrapper = new EntityWrapper();
        if(EmptyUtil.isNotEmpty(dto.getPhone()))
            wrapper.eq("phone" ,dto.getPhone());
        Register findCustomer = registerService.selectOne(wrapper);
        if(EmptyUtil.isNotEmpty(findCustomer)){

        }else{
            /*if(EmptyUtil.isNotEmpty(dto.getPassword())) {
                Object voucher = dto.getPassword();
                String salt = UUID.randomUUID().toString();
                Object simpleHash = new SimpleHash(encryption_type, voucher, salt, hashIterations);
                String password = simpleHash.toString();
                token = JWTUtil.sign(dto.getPhone() + "", password, findCustomer.getId(), jwtTime);
                Subject user = SecurityUtils.getSubject();
                user.login(new JWTToken(token));
                dto.setPassword(password);
                registerService.register(dto,salt);
            }*/

        }
        if(StringUtils.isNotBlank(token)){
            //如果登陆成功将token记录到redis中
            redisUtil.set(RedisConstants.REDIS_TOKEN +findCustomer.getPhone()+"",token,TOKEN_LOSE);
        }
        return BaseResult.successData(token);
    }

    /**
     * 发送短信验证码
     * @return
     */
    @RequestMapping("/sendSmsCode")
    @ResponseBody
    public BaseResult sendSmsCode(@Valid @RequestBody RegisterDto dto,BindingResult result){
        if (result.hasErrors())
            return BaseResult.failMsg(result.getFieldError().getDefaultMessage());


        return BaseResult.success();
    }



    /**
     * 发送短信验证码
     * @return
     */
    @RequestMapping("/verifySmsCode")
    @ResponseBody
    public BaseResult verifySmsCode(@Valid @RequestBody SmsDto smsDto,BindingResult result){
        if (result.hasErrors())
            return BaseResult.failMsg(result.getFieldError().getDefaultMessage());


        return BaseResult.success();
    }






}
